
package FLS.Controller;


import java.util.Date;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import FLS.DataAccessObjects.KundeDao;
import FLS.Model.Kunde;

@Controller
public class KundeCtrl {
private static Logger log = Logger.getLogger(KundeCtrl.class);
private KundeDao kundeDao = null;

public void setKundeDao(KundeDao gastDao) {
this.kundeDao = kundeDao;
}



/**
* <p>Gast form request.</p>
* @param id
* <p>Expected HTTP GET and request '/EditGast.html'.</p>
* @return mv;
*/
@RequestMapping(value="/EditKunde.html", method=RequestMethod.GET)
public ModelAndView edit(@RequestParam(required=false) Integer id) {
log.debug("EditKunde.html");
ModelAndView mv = new ModelAndView();
if (id == null) {
mv.addObject(new Kunde());
} else { 
mv.addObject(kundeDao.findById(id));
}
mv.setViewName("edit-kunde");
return mv;
}

/**
* <p>Saves a gast.</p>
* @param gast
* @param model
* <p>Expected HTTP POST and request '/SaveKunde.html'.</p>
* @return "redirect:AlleKunde.html"
*/
@RequestMapping(value="/SaveKunde.html", method=RequestMethod.POST)
public String save(Kunde kunde, Model model) {

Kunde result = kundeDao.save(kunde);
// set id from create
if (kunde.getId() == null) {
kunde.setId(result.getId());
}
model.addAttribute("statusMessageKey", "kunde.form.msg.success");
return "redirect:AlleKunde.html";
}

/**
* <p>Deletes a kunde.</p>
* @param gast
* <p>Expected HTTP POST and request '/LoescheGast.html'.</p>
*/
@RequestMapping(value="/LoescheKunde.html", method=RequestMethod.GET)
public String delete(Kunde kunde) {
kundeDao.delete(kunde);
return "redirect:AlleKunde.html";
}

/**
* <p>Searches for all gaeste and returns them in a 
* <code>Collection</code>.</p>
* @return mv
* <p>Expected HTTP GET and request '/AlleGaeste.html'.</p>
*/
@RequestMapping(value="/AlleKunde.html", method=RequestMethod.GET)
public ModelAndView findAll() {
ModelAndView mv = new ModelAndView();
mv.addObject("title", "Kunden");
mv.addObject("message", "Alle Kunden");
mv.addObject("kunden", kundeDao.findAll());
mv.setViewName("list-kunden");
return mv;
}

}